package com.ggb.smartstudy.controller.admin;

import com.ggb.smartstudy.entity.CourseCategory;
import com.ggb.smartstudy.service.CourseCategoryService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gefangjie
 */
@RestController
@RequestMapping("/admin/courseCategory")
@Validated
public class CourseCategoryController {

    private final CourseCategoryService courseCategoryService;

    public CourseCategoryController(CourseCategoryService courseCategoryService) {
        this.courseCategoryService = courseCategoryService;
    }

    // 获取所有课程分类信息
    @GetMapping("/getAll")
    public ResponseEntity<List<CourseCategory>> getAllCourseCategories() {
        List<CourseCategory> courseCategories = courseCategoryService.getAllCourseCategories();
        return ResponseEntity.ok(courseCategories);
    }

    // 通过课程分类ID获取课程分类信息
    @GetMapping("/{id}")
    public ResponseEntity<CourseCategory> getCourseCategoryById(@PathVariable Integer id) {
        CourseCategory courseCategory = courseCategoryService.getCourseCategoryById(id);
        if (courseCategory != null) {
            return ResponseEntity.ok(courseCategory);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 添加新课程分类
    @PostMapping("/add")
    public ResponseEntity<CourseCategory> addCourseCategory(@Valid @RequestBody CourseCategory courseCategory) {
        courseCategoryService.insertCourseCategory(courseCategory);
        return ResponseEntity.ok(courseCategory);
    }

    // 更新课程分类信息
    @PutMapping("/{id}")
    public ResponseEntity<CourseCategory> updateCourseCategory(@PathVariable Integer id, @Valid @RequestBody CourseCategory courseCategory) {
        courseCategory.setId(id);
        courseCategoryService.updateCourseCategory(courseCategory);
        return ResponseEntity.ok(courseCategory);
    }

    // 删除课程分类
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCourseCategory(@PathVariable Integer id) {
        courseCategoryService.deleteCourseCategory(id);
        return ResponseEntity.noContent().build();
    }
}
